package com.powerunion.datacollection.web.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.powerunion.datacollection.service.TransmitTaskManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class TransmitTaskAction extends DispatchAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(TransmitTaskAction.class);
	private  TransmitTaskManager ttManager = null;
	
	public TransmitTaskAction() {
		super();
		this.ttManager = new TransmitTaskManager();
	}
	
	public void finalize() throws Throwable {
		this.ttManager = null;
		super.finalize();
	}
	
	
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userCode = request.getRemoteUser();
		String saveStr = request.getParameter("paramStr");
		Transaction transaction = null;
		response.setContentType("text/json;charset=utf-8");
		try{
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			boolean flag = this.ttManager.save(saveStr, userCode);
			
			if (flag == true) {
				response.getWriter().print("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}
			
			transaction.commit();
		} catch (Exception e) {
			logger.error("", e);
			try {
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}
		return null;
	}
	
}
